package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.add(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        if (checkitemIds!=null&&checkitemIds.length>0) {
            for (Integer checkItemId : checkitemIds) {
                checkGroupDao.addCheckgroup_Checkitem(checkGroupId,checkItemId);
            }
        }

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);

        if(queryString!=null&&queryString.length()>0){
            queryString="%"+queryString+"%";
        }
        List<CheckGroup> checkGroupList=checkGroupDao.findPage(queryString);

        PageInfo<CheckGroup> pageInfo = new PageInfo<CheckGroup>(checkGroupList);
        long total = pageInfo.getTotal();
        return new PageResult(total,checkGroupList);
    }

    @Override
    public CheckGroup findCheckGroupById(Integer id) {
        CheckGroup checkGroup=checkGroupDao.findCheckGroupById(id);
        return checkGroup;
    }

    @Override
    public List<Integer> findCheckGroupId_CheckItemIdById(Integer id) {
        List<Integer> integerList=checkGroupDao.findCheckGroupId_CheckItemIdById(id);
        return integerList;
    }

    @Override
    public void update(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.update(checkGroup);
        checkGroupDao.deleteCorrelation(checkGroup.getId());
        if(checkitemIds.length>0&&checkitemIds!=null){
            checkGroupDao.updateCorrelation(checkGroup,checkitemIds);
        }
    }

    @Override
    public void del(Integer id) {
        checkGroupDao.deleteCorrelation(id);
        checkGroupDao.del(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> checkGroupList=checkGroupDao.findAll();
        return checkGroupList;
    }

    @Override
    public List<CheckGroup> findAllCheckGroup() {
        List<CheckGroup> checkGroupList=checkGroupDao.findAllCheckGroup();
        return checkGroupList;
    }
}
